package com.lnyt.util;

import com.lnyt.pojo.ticket.TicketTask;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

// Http请求工具类
public class TicketUtil {
    // Url添加参数
    public static String urlWithPrams(String url, Map<String, String> pram) {
        if (pram == null) {
            return "";
        }

        StringBuilder result = new StringBuilder();

        for (Map.Entry<String, String> entry : pram.entrySet()) {
            result.append("&");
            result.append(entry.getKey());

            if (entry.getValue() != null) {
                result.append("=");
                result.append(entry.getValue());
            }
        }

        if (result.length() != 0) {
            result.setCharAt(0, '?');
        }

        return url + result.toString();
    }

    // 得到Html中 var 值
    public static Map<String, String> getHtmlVarMap(String htmlData) {
        Map<String, String> result = new HashMap<>();
        int pos1 = 0;
        int pos2 = 0;

        while (pos1 != -1 && pos2 != -1) {
            pos1 = htmlData.indexOf("var ", pos2);
            if (pos1 == -1)
                break;

            pos2 = htmlData.indexOf(";", pos1);
            String item = htmlData.substring(pos1, pos2);

            String regex = "(var\\s+)(.+)(\\s*=\\s*)(.+)";
            String key = item.replaceAll(regex, "$2");
            String value = item.replaceAll(regex, "$4");

            if (!value.toLowerCase().equals("null")) {
                if (value.startsWith("'")) {
                    value = value.substring(1, value.length() - 1);
                }
                result.put(key.trim(), value);
            }
        }

        return result;
    }

    // 得到Json字符串中的json值
    public static String getStringJson(String key, String data) {
        int pos1 = 0;
        int pos2 = 0;
        String value = null;

        pos1 = data.indexOf(key);
        if (pos1 != -1) {
            pos1 = data.indexOf(":", pos1);
            if (pos1 != -1) {
                pos2 = data.indexOf(",", pos1);
                if (pos2 == -1) {
                    pos2 = data.length();
                }
                value = data.substring(pos1 + 1, pos2).trim();
                if (value.startsWith("'") && value.endsWith("'")) {
                    value = value.substring(1, value.length() - 1);
                }
            }
        }

        return value;
    }

    // 当前时间
    public static String getDate() {
        String curDate = null;
        SimpleDateFormat SimpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            curDate = SimpleDate.format(new Date());
        } catch (Exception exc) {
            exc.printStackTrace();
        }

        return curDate;
    }

    // 时间格式化
    public static String getDateGMT(String oldDate) {
        String newDate = null;
        Date date = null;
        SimpleDateFormat oldSimpleDate = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat newSimpleDate = new SimpleDateFormat(
                "EEE MMM dd yyyy '00:00:00' 'GMT'Z '(中国标准时间)'",
                Locale.ENGLISH);

        try {
            date = oldSimpleDate.parse(oldDate);
            newDate = newSimpleDate.format(date);
        } catch (Exception exc) {
            exc.printStackTrace();
        }

        return newDate;
    }

    // 乘客订单字符串 old
    public static String getOldPassengerStr(TicketTask ticketTask) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(ticketTask.getPassengerName());
        stringBuffer.append(",");
        stringBuffer.append(ticketTask.getPassengerType());
        stringBuffer.append(",");
        stringBuffer.append(ticketTask.getPassengerId());
        stringBuffer.append(",");
        stringBuffer.append(ticketTask.getPassengerIdType());

        return stringBuffer.toString();
    }

    // 乘客订单字符串
    public static String getPassengerTicketStr(TicketTask ticketTask, String ticketType) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(ticketType);
        stringBuffer.append(",0,");
        stringBuffer.append(ticketTask.getPassengerType());
        stringBuffer.append(",");
        stringBuffer.append(ticketTask.getPassengerName());
        stringBuffer.append(",");
        stringBuffer.append(ticketTask.getPassengerIdType());
        stringBuffer.append(",");
        stringBuffer.append(ticketTask.getPassengerId());
        stringBuffer.append(",");
        stringBuffer.append(ticketTask.getCellPhone());
        stringBuffer.append(",N");

        return stringBuffer.toString();
    }

    // 计算订单信息Md5
    public static String calcTrainMd5(TicketTask ticketTask) {
        String trainHashSorce = ticketTask.getTrainDate() + "-" + ticketTask.getTrainNumber() + "-"
                + ticketTask.getFromStation() + "-" + ticketTask.getToStation();

        return CheckUtil.calcMd5(trainHashSorce);
    }
}